/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.usercenter.domain.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.DateUtil;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.usercenter.domain.domain.UserDomain;
import cn.hallele.ms.usercenter.domain.domain.UserQueryDomain;
import cn.hallele.ms.usercenter.model.cache.UserCache;
import cn.hallele.ms.usercenter.model.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * hallele_ms_usercenter
 *
 * @author anle5
 * @since 2024/10/5 16:15
 */
@Slf4j
@Service
public class UserDEDas extends ModelDas<UserCache, UserDomain, UserEntity> {
    protected UserDEDas(UserCache option) {
        super(option);
    }

    /**
     * 分页查询用户
     *
     * @param userQueryDomain 用户信息
     * @return 分页用户结果
     */
    public Page<UserDomain> listUser(UserQueryDomain userQueryDomain) {
        int pageNo = Optional.ofNullable(userQueryDomain.getPageNo()).orElse(Page.DEFAULT_PAGE_NO);
        int pageSize = Optional.ofNullable(userQueryDomain.getPageSize()).orElse(Page.DEFAULT_PAGE_SIZE);
        pageNo = Math.max(pageNo, 1) - 1;
        pageSize = Math.min(Page.MAX_PAGE_SIZE, Math.max(pageSize, 1));

        if (userQueryDomain.getStartTime() == null) {
            // 默认查询1年以内的
            userQueryDomain.setStartTime(DateUtil.offsetSecond(DateUtil.nowDate(), -365 * 24 * 60 * 60));
        }

        if (userQueryDomain.getEndTime() == null) {
            userQueryDomain.setEndTime(DateUtil.nowDate());
        }

        return revertPage(this.option.findAll(convert(userQueryDomain), PageRequest.of(pageNo, pageSize)));
    }

    /**
     * 通过用户名查询用户
     *
     * @param username 用户名
     * @return 用户信息
     */
    public UserDomain getUser(String username) {
        return revert(this.option.findByUsername(username));
    }

    /**
     * 通过id查询用户
     *
     * @param ids id集合
     * @return 用户信息
     */
    public List<UserDomain> findByIdIn(List<Long> ids) {
        return revertList(this.option.findByIdIn(ids));
    }

    /**
     * 通过id查询用户
     *
     * @param id id
     * @return 用户信息
     */
    public UserDomain findById(Long id) {
        return revert(this.option.findOneById(id));
    }

    /**
     * 添加用户
     *
     * @param userDomain 用户信息
     */
    public void addUser(UserDomain userDomain) {
        this.option.saveUser(convert(userDomain));
    }

    /**
     * 修改用户
     *
     * @param userDomain 用户
     * @return 修改后的用户信息
     */
    public UserDomain updateUser(UserDomain userDomain) {
        return revert(this.option.saveUser(convert(userDomain)));
    }

    /**
     * 删除用户
     *
     * @param username 用户名
     */
    public void deleteUser(String username) {
        this.option.deleteUser(username);
    }

    /**
     * 将T转成E
     *
     * @param userDomain 被转换者
     * @return S 转换后者
     */
    @Override
    protected UserEntity convert(UserDomain userDomain) {
        return ObjectUtil.map(userDomain, u -> UserEntity.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .username(u.getUsername())
                .password(u.getPassword())
                .avatarId(u.getAvatarId())
                .phoneNumber(u.getPhoneNumber())
                .email(u.getEmail())
                .sex(u.getSex())
                .age(u.getAge())
                .status(u.getStatus())
                .personalitySignature(u.getPersonalitySignature())
                .passExpiredTime(u.getPassExpiredTime())
                .accountExpiredTime(u.getAccountExpiredTime())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .createBy(u.getCreateBy())
                .updateBy(u.getUpdateBy())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param userEntity 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected UserDomain revert(UserEntity userEntity) {
        return ObjectUtil.map(userEntity, u -> UserDomain.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .username(u.getUsername())
                .password(u.getPassword())
                .avatarId(u.getAvatarId())
                .phoneNumber(u.getPhoneNumber())
                .email(u.getEmail())
                .sex(u.getSex())
                .age(u.getAge())
                .status(u.getStatus())
                .personalitySignature(u.getPersonalitySignature())
                .passExpiredTime(u.getPassExpiredTime())
                .accountExpiredTime(u.getAccountExpiredTime())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .createBy(u.getCreateBy())
                .updateBy(u.getUpdateBy())
                .build());
    }

    /**
     * 转换 UserQueryDomain -> UserQueryQO
     *
     * @param userQueryDomain UserQueryDomain
     * @return UserQueryBO
     */
    private UserEntity convert(UserQueryDomain userQueryDomain) {
        return ObjectUtil.map(userQueryDomain, u -> UserEntity.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .username(u.getUsername())
                .phoneNumber(u.getPhoneNumber())
                .status(u.getStatus())
                .build());
    }
}
